package com.study.service;
/**
 * @author： li
 * @date： 2021-05-18 18:25
 * @version 1.0
 */

import com.study.pojo.user;
import com.study.util.DBUtil1;
import com.study.dao.userdao;
import com.study.util.PageUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class userservice {
    user us=new user();
    DBUtil1 db=new DBUtil1();
    //用户注册
    public static int register(user user){
        int i=0;
        try {
            i= userdao.insert(user);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return i;
    }
    //用户登入
    public static int login(user user) throws SQLException {
        ResultSet rs=null;
        try {
           rs= userdao.selectbyusernameandpassword(user);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        if(rs.next()){
            return 1;
        }else {
        return 0;
        }
    }
    //用户修改
    public static  int update(user user){
int i=0;
        try{
            i=userdao.update(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;

    }
    //用户删除
    public static int delete(user user) {
        int i = 0;
try{
    i=userdao.delete(user);
} catch (Exception e) {
    e.printStackTrace();
}
        return i;
    }
//用户列表
    public static List<user> list(){
        List<user> user=new ArrayList<>();
        try{
            user=userdao.result();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    public static int deletebyid(user user) {
        int i=0;
        try{
            userdao.deletebyid(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
   /* public static List<user>  splitList(List<user> list, int pageSize)
    {
        int listSize = list.size();
        int page = (listSize + (pageSize - 1)) / pageSize;

        List<user> listArray = new ArrayList<user>();
        for (int i = 0; i < page; i++) {
            List<user> subList = new ArrayList<user>();
            for (int j = 0; j < listSize; j++) {
                int pageIndex = ((j + 1) + (pageSize - 1)) / pageSize;
                if (pageIndex == (i + 1)) {
                    subList.add(list.get(j));
                }
                if ((j + 1) == ((j + 1) * pageSize))
                {
                    break;
                }
            }
            listArray.add((user)subList);
        }
        return listArray;
    }*/
    /**
     * 分页方法
     * @param start
     * @param end
     * @return
     */
    public List<user> pagination(Integer start, Integer end) {
        List<user> userList = null;
        try {
            userList = userdao.selectForPagination(start,end);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }


    /**
     * 获取最大页数
     * @param pageSize
     * @return
     */
    public static Integer maxPage(Integer pageSize){
        Integer page = 0;
        try {
            Integer count = userdao.countNum();
            page = (int)Math.ceil((double)count/pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return page;
    }


    /**
     * 通过工具类使用集合进行分页
     * @param pageSize
     * @param pageNum
     * @return
     */
    public static List<user> page(Integer pageSize, Integer pageNum){
        List<user> page = null;
        try {
            page = PageUtil.page(userdao.result(), pageSize, pageNum);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return page;
    }
}
